<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

use think\Config;

class Cron extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        // parent::_initialize();
    }

    // 算力
    public function index()
    {
        set_time_limit(0);
        ignore_user_abort(true);

        // 共享算力 所有团队的业绩
        $listTeamAmount = model('User')->getTeamAmount();

        // 返佣日期
        $today = date('Ymd');

        // 等级配置信息
        $group_list = db('UserGroup')->column('id, static_rate, dynamic_max_num');

        /*--------------------------------------------------------------------*/
        // 静态算力
        $map = [
            'static_rebate_date'=>['lt', $today],
            'score'=>['gt', 0],
        ];
        $list = db('User')->field('id, group_id, score, money')->where($map)->select();
        if ($list) {
            foreach ($list as $key => $value) {
                if (!isset($group_list[$value['group_id']])) {
                    continue;
                }
                // 算力
                $rate = $group_list[$value['group_id']]['static_rate']/100;
                $money = $value['score']*$rate;
                model('app\common\model\User')->staticRebate($value['id'], $money, $value['money'], $today);
                // 减少货币余量
                model('Config')->where('name', 'bkc_balance')->setDec('value', $money);
            }
        }
        /*--------------------------------------------------------------------*/

        //财富宝资产+智能宝资产总量小于此设置时，动态算力自动停止。
        $user_min_total_bkc = (int)config::get('site.user_min_total_bkc');

        // 共享算力比例
        $share_rebate_rate = (float)config::get('site.share_rebate_rate');

        // 分享算力
        $map = [
            'u1.dynamic_rebate_date'=>['lt', $today],
            // 'u2.group_id'=>['egt', 3],
        ];
        // 达到分享算力条件的会员的2星下线数
        $listChildNum = db('User u1')->join('__USER__ u2', 'u1.id=u2.parent_id')->where($map)->where(['u2.group_id'=>['egt', 3]])->where('u1.score+u1.money>='.$user_min_total_bkc)->group('u1.id')->having('child_num>0')->column('u1.id, count(u2.id) as child_num');

        // 达到分享算力条件的会员
        // $list = db('User u1')->field('u1.id, u1.group_id, count(u2.id) as child_num, u1.money')->join('__USER__ u2', 'u1.id=u2.parent_id')->where($map)->where('u1.score+u1.money>='.$user_min_total_bkc)->group('u1.id')->having('child_num>0')->select();
        $list = db('User u1')->field('u1.id, u1.group_id, u1.money')->where($map)->where('u1.score+u1.money>='.$user_min_total_bkc)->select();

        if ($list) {
            foreach ($list as $key => $value) {
                if (!isset($group_list[$value['group_id']])) {
                    continue;
                }
                $total_money = 0;
                // 动态日封顶算力
                $max_num = $group_list[$value['group_id']]['dynamic_max_num'];

                /*------------------------------------------------------------------------------*/
                $child_num = isset($listChildNum[$value['id']]) ? $listChildNum[$value['id']] : 0;
                if ($child_num>0) {
                    // 下线代数
                    $child_num = $child_num>3 ? 3 : $child_num;
                    // 分享算力配置
                    $dynamic_rebate = [
                        1=>0.2,
                        2=>0.07,
                        3=>0.03,
                    ];
                    // 下线
                    $children = model('user')->getChildren($value['id'], $child_num);
                    // 算力
                    $total_money = 0;
                    for ($i=1; $i <= $child_num; $i++) {
                        if (!isset($children[$i])) {
                            break;
                        }
                        foreach ($children[$i] as $k => $v) {
                            if (!isset($group_list[$v['group_id']])) {
                                continue;
                            }
                            //              下线的智能宝 * 下线的静态算力比例                           * 当前用户的动态算力比例
                            $total_money += $v['score'] * $group_list[$v['group_id']]['static_rate']/100 * $dynamic_rebate[$i];
                        }

                        /*echo'<pre>';
                        print_r($total_money);
                        echo'</pre>';*/
                    }

                    /*echo'<pre>';
                    print_r($children);
                    print_r($total_money);
                    echo'</pre>';
                    die;*/
                    if ($total_money>$max_num) {
                        $total_money = $max_num;
                        $max_num = 0;
                    } else {
                        $max_num = $max_num - $total_money;
                    }
                    model('app\common\model\User')->dynamicRebate($value['id'], $total_money, $value['money'], $today);
                    // 减少货币余量
                    model('Config')->where('name', 'bkc_balance')->setDec('value', $total_money);
                }
                /*------------------------------------------------------------------------------*/

                // 共享算力
                if (isset($listTeamAmount[$value['id']])) {
                    $teamAmount = $listTeamAmount[$value['id']];
                    // 有业绩，且有小区业绩
                    if (isset($teamAmount['amount']) && count($teamAmount['amount'])==2) {
                        $share_total_money = $total_money;
                        $total_money = min($teamAmount['amount'])*$share_rebate_rate;
                        $total_money = $total_money>$max_num ? $max_num : $total_money;
                        model('app\common\model\User')->dynamicRebate($value['id'], $total_money, $value['money']+$share_total_money, $today, 3);
                        // 减少货币余量
                        model('Config')->where('name', 'bkc_balance')->setDec('value', $total_money);
                    }
                }

            }
        }


        model('Config')->refreshFile();

        return '执行成功!';
    }
}
